import api from 'src/api'
import {getCookie} from 'util/util'
const state = {
  lists: [],
  followLists:[],
  hottests:[]
}

const getters = {
  getLists: state =>state.lists,
  followLists:state=>state.followLists,
  hottests:state=>state.hottests
}

const mutations = {
  SET_LIST: (state, lists) => {
    state.lists.push(...lists)
  },
  SET_FOLLOW_LIST:(state,lists)=>{
  	state.followLists.push(...lists)
  },
  SET_HOTTESTS:(state,lists)=>{
  	state.hottests = lists
  },
}

const actions = {
  getLists ({ state, commit },page) {
  	return new Promise(function(resolve,reject){
    	api.get('/post/hot?page='+page).then((response) => {
		   	response.data.forEach(item => {
		        item.tagList = item.tag.split(";");
		        if(item.images){
		        	item.images = item.images.split(",");	
		        }
		    });
		    commit('SET_LIST', response.data)
		    resolve(response)
		  }).catch((error) => {
		  	reject(error)
      	console.log(error)
    	})
    })
  },
  getFollowLists({ state, commit },page){
  	return new Promise(function(resolve,reject){
    	api.get("timeline?api_token=" + getCookie('UNION_TOKEN') + "&page="+page).then((response) => {
		   	response.data.forEach(item => {
		        item.tagList = item.tag.split(";");
		        if(item.images){
		        	item.images = item.images.split(",");	
		        }
		    });
		    commit('SET_FOLLOW_LIST', response.data)
		    resolve(response)
		  }).catch((error) => {
		  	reject(error)
      	console.log(error)
    	})
    })
  },
  getHottests({ state, commit }){
   	return new Promise(function(resolve,reject){
    	api.get("/post/hottest").then((response) => {
		   	response.data.forEach(item => {
		        item.tagList = item.tag.split(";");
		        if(item.images){
		        	item.images = item.images.split(",");	
		        }
		    });
		    commit('SET_HOTTESTS', response.data)
		    resolve(response)
		  }).catch((error) => {
		  	reject(error)
      	console.log(error)
    	})
    })	
  }
}

export default {
  state,
  getters,
  mutations,
  actions
}
